package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.domain.SubjectRequest;
import com.pureut.system.domain.SubjectRequestDetails;
import com.pureut.system.domain.SupplierAccess;
import com.pureut.system.domain.SupplierList;
import com.pureut.system.domain.dto.SubjectRequestDetailsDto;
import com.pureut.system.domain.dto.SubjectRequestInitDto;
import com.pureut.system.domain.dto.SubjectRequestPagesDto;
import com.pureut.system.domain.vo.SubjectRequestVo;
import com.pureut.system.mapper.SubjectRequestMapper;
import com.pureut.system.service.IMaterialAwmateriaService;
import com.pureut.system.service.ISubjectRequestDetailsService;
import com.pureut.system.service.ISubjectRequestService;
import com.pureut.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-05 15:50:50
 * @Desc: 实验品申请
 */
@Service("subjectRequestService")
public class SubjectRequestServiceImpl extends ServiceImpl<com.pureut.system.mapper.SubjectRequestMapper, SubjectRequest> implements ISubjectRequestService {

    /**
     * 实验品申请Dao
     */
    @Resource
    private SubjectRequestMapper subjectRequestMapper;

    @Resource
    ISysUserService sysUserService;

    @Resource
    FeignService feignService;

    @Resource
    private IMaterialAwmateriaService materialAwmateriaService;

    /**
     * 申请明细
     */
    @Resource
    private ISubjectRequestDetailsService subjectRequestDetailsService;

    /**
     * 查询列表
     * @param subjectRequestVo
     * @return
     */
    @Override
    public List<SubjectRequestPagesDto> queryPage(SubjectRequestVo subjectRequestVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("experiment_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SubjectRequestPagesDto> subjectRequestPagesDtos = subjectRequestMapper.selectByPage(subjectRequestVo);
        subjectRequestPagesDtos.forEach(e->{
            e.setExperimentType(netMap.get(String.valueOf(e.getExperimentType())));
        });
        return subjectRequestPagesDtos;

    }


    /**
     * 新增实验品申请
     * @param subjectRequestVo
     * @return
     */
    @Transactional
    @Override
    public Boolean save(SubjectRequestVo subjectRequestVo) {
        SubjectRequest subjectRequest = new SubjectRequest();
        BeanUtil.copyProperties(subjectRequestVo, subjectRequest);
        subjectRequest.setCreateBy(SecurityUtils.getUsername());
        subjectRequest.setCreateTime(new Date());
        subjectRequest.setProposerBy(SecurityUtils.getUserId());
        subjectRequest.setStatus("1");
        boolean save = save(subjectRequest);
        if(save){
            if(ObjectUtil.isNotEmpty(subjectRequestVo.getSubjectRequestDetailsList())){
                List<SubjectRequestDetails> collect = subjectRequestVo.getSubjectRequestDetailsList().stream().map(e -> {
                    SubjectRequestDetails subjectRequestDetails = new SubjectRequestDetails();
                    subjectRequestDetails.setCreateBy(SecurityUtils.getUsername());
                    subjectRequestDetails.setCreateTime(new Date());
                    subjectRequestDetails.setMaterielId(e.getMaterielId());
                    subjectRequestDetails.setSubjectRequestId(subjectRequest.getId());
                    return subjectRequestDetails;
                }).collect(Collectors.toList());
                subjectRequestDetailsService.saveBatch(collect);
            }
        }
        return save;
    }


    /**
     * 更新实验品申请
     * @param subjectRequestVo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateById(SubjectRequestVo subjectRequestVo) {
        SubjectRequest subjectRequest = new SubjectRequest();
        BeanUtil.copyProperties(subjectRequestVo, subjectRequest);
        subjectRequest.setUpdateBy(SecurityUtils.getUsername());
        subjectRequest.setUpdateTime(new Date());
        boolean b = updateById(subjectRequest);
        if(b){
            Map<String,Object> map=new HashMap<>();
            map.put("subject_request_id",subjectRequestVo.getId());
            subjectRequestDetailsService.removeByMap(map);
            if(ObjectUtil.isNotEmpty(subjectRequestVo.getSubjectRequestDetailsList())){
                List<SubjectRequestDetails> collect = subjectRequestVo.getSubjectRequestDetailsList().stream().map(e -> {
                    SubjectRequestDetails subjectRequestDetails = new SubjectRequestDetails();
                    subjectRequestDetails.setCreateBy(SecurityUtils.getUsername());
                    subjectRequestDetails.setCreateTime(new Date());
                    subjectRequestDetails.setMaterielId(e.getMaterielId());
                    subjectRequestDetails.setSubjectRequestId(subjectRequest.getId());
                    return subjectRequestDetails;
                }).collect(Collectors.toList());
                subjectRequestDetailsService.saveBatch(collect);
            }
        }
        return b;
    }

    /**
     * 获取实验品申请 初始化信息
     * @return
     */
    @Override
    public SubjectRequestInitDto infoInit(){
        SubjectRequestInitDto subjectRequestInitDto=new SubjectRequestInitDto();
        String sn = feignService.getAuthorityCoding("warehouse:outbound:reworksto:list");
        if (sn == null) {
            throw new GlobalException("该单据未进行配置,无法生成单据编码");
        }
        subjectRequestInitDto.setLabItemCode(sn);
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        subjectRequestInitDto.setProposerBy(sysUser.getNickName());
        subjectRequestInitDto.setApplicationDate(new Date());
        subjectRequestInitDto.setMaterialClassificationList(materialAwmateriaService.selectChildrenListThe2());
        return subjectRequestInitDto;
    }

    /**
     * 获取实验品申请详细信息
     * @param id
     * @return
     */
    @Override
    public SubjectRequestDetailsDto getInfoById(Long id){
        SubjectRequestDetailsDto subjectRequestDetailsDto=new SubjectRequestDetailsDto();
        BeanUtil.copyProperties(getById(id), subjectRequestDetailsDto);
        subjectRequestDetailsDto.setSubjectRequestDetailsList(subjectRequestDetailsService.getListByPid(id));
        return subjectRequestDetailsDto;
    }

    /**
     * 审核
     * @param subjectRequestVo
     * @return
     */
    @Override
    public Boolean examine(SubjectRequestVo subjectRequestVo){
        SubjectRequest subjectRequest = new SubjectRequest();
        subjectRequest.setId(subjectRequestVo.getId());
        if(subjectRequestVo.getAuditState()==1){
            subjectRequest.setStatus("3");
        }else{
            subjectRequest.setStatus("4");
        }
        boolean b = updateById(subjectRequest);
        return b;
    }
}
